package com.servlet;

import com.beans.AdminInfo;
import com.beans.PageInfo;
import com.util.PageUtil;
import com.dao.AdminDao;
import com.util.Des;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "AdminServlet.do", value = "/AdminServlet.do")
public class AdminServlet extends HttpServlet {
    AdminDao adminDao = new AdminDao();

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String flag = request.getParameter("flag");
        if ("add".equals(flag)) {
            addAdmin(request, response);
        } else if ("checkAdminName".equals(flag)) {
            checkAdminName(request, response);
        } else if ("getAdminList".equals(flag)) {
            getAdminList(request, response);
        } else if ("logout".equals(flag)) {
            logout(request, response);
        } else if ("refresDate".equals(flag)) {
            refresDate(request, response);
        } else if ("lock".equals(flag)) {
            lock(request, response);
        } else if ("unlock".equals(flag)) {
            unlock(request, response);
        } else if ("deleteAdmin".equals(flag)) {
            deleteAdmin(request, response);
        }else if ("updateAdmin".equals(flag)){
            updateAdmin(request,response);
        }else if ("doUpdateAdmin".equals(flag)){
            doUpdateAdmin(request,response);
        }else if ("checkOldPassword".equals(flag)){
            checkOldPassword(request,response);
        }else if ("updateAdminPassword".equals(flag)){
            updateAdminPassword(request,response);
        }
    }

    //修改自己账户的密码
    private void updateAdminPassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String password = request.getParameter("newpassword");

        int result = adminDao.updateAdminPasswordById(Des.encStr(password),id);

        if (result==1){
            request.setAttribute("msg","<script>alert('密码修改成功')</script>");
            request.getRequestDispatcher("/admin/password_edit.jsp").forward(request,response);
        }else {
            request.setAttribute("msg","<script>alert('密码修改失败')</script>");
            request.getRequestDispatcher("/admin/password_edit.jsp").forward(request,response);
        }

    }

    //修改自己账户密码时，检查旧密码是否正确
    private void checkOldPassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String oldpassword = request.getParameter("oldpassword");

        AdminInfo admin = adminDao.getAdminInfoById(id);

        if (oldpassword.equals(Des.desStr(admin.getPassword()))){
            response.getWriter().print("1");
        }else {
            response.getWriter().print("0");
        }
    }

    //执行修改用户
    private void doUpdateAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String adminName = request.getParameter("adminName");
        String note = request.getParameter("note");

        int result = adminDao.updateAdminNameAndNoteById(id,adminName,note);
        AdminInfo adminInfo = adminDao.getAdminInfoById(id);
        if (result==1){
            request.setAttribute("msg","<script>alert('用户修改成功')</script>");
            request.setAttribute("adminInfo",adminInfo);
            request.getRequestDispatcher("/admin/admin_update.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","<script>alert('用户修改失败')</script>");
            request.setAttribute("adminInfo",adminInfo);
            request.getRequestDispatcher("/admin/admin_update.jsp").forward(request,response);
        }
    }

    //修改用户信息的中转servlet
    private void updateAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String pageIndex = request.getParameter("pageIndex");
        //修改的用户的角色信息
        AdminInfo adminInfo = adminDao.getAdminInfoById(id);
        //当前登录账号的角色信息
        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("session_admin");
        if (admin.getRoleId() < adminInfo.getRoleId() || adminInfo.getRoleId() == 0 ||admin.getRoleId()==1) {
            request.setAttribute("pageIndex",pageIndex);
            request.setAttribute("adminInfo",adminInfo);
            request.getRequestDispatcher("/admin/admin_update.jsp").forward(request,response);
        }else {
            request.setAttribute("pageIndex",pageIndex);
            request.setAttribute("msg", "<script>alert('权限不足')</script>");
            this.getAdminList(request, response);
        }
    }

    //通过id删除用户
    private void deleteAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int roleId = Integer.parseInt(request.getParameter("roleId"));

        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("session_admin");
        if (admin.getRoleId() < roleId || roleId == 0  ||admin.getRoleId()==1) {
            int result = adminDao.deleteAdminByid(id);
            if (result == 1) {
                this.getAdminList(request, response);
            } else {
                request.setAttribute("msg", "<script>alert('删除用户失败')</script>");
                this.getAdminList(request, response);
            }
        } else {
            request.setAttribute("msg", "<script>alert('权限不足')</script>");
            this.getAdminList(request, response);
        }
    }

    //解锁锁定的用户
    private void unlock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int roleId = Integer.parseInt(request.getParameter("roleId"));

        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("session_admin");
        if (admin.getRoleId() < roleId || roleId == 0  ||admin.getRoleId()==1) {
            int result = adminDao.unlockAdminById(id);
            if (result == 1) {
                this.getAdminList(request, response);
            } else {
                request.setAttribute("msg", "<script>alert('解锁用户失败')</script>");
                this.getAdminList(request, response);
            }
        } else {
            request.setAttribute("msg", "<script>alert('权限不足')</script>");
            this.getAdminList(request, response);
        }
    }

    //锁定用户
    private void lock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int roleId = Integer.parseInt(request.getParameter("roleId"));

        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("session_admin");
        if (admin.getRoleId() < roleId || roleId == 0  ||admin.getRoleId()==1) {
            int result = adminDao.lockAdminById(id);
            if (result == 1) {
                this.getAdminList(request, response);
            } else {
                request.setAttribute("msg", "<script>alert('锁定用户失败')</script>");
                this.getAdminList(request, response);
            }
        } else {
            request.setAttribute("msg", "<script>alert('权限不足')</script>");
            this.getAdminList(request, response);
        }

    }

    //刷新前台显示时间
    private void refresDate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String s = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss").format(new Date());

        AdminInfo admin = (AdminInfo) request.getSession().getAttribute("session_admin");
        request.getSession().setAttribute("session_admin", admin);
        response.getWriter().print(s);
    }

    //登出
    private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().invalidate();
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }

    //分页
    private void getAdminList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize = 10;
        int rowCount = adminDao.getAdminCount();
        int pageIndex = 1;
        String pageIndexstr = request.getParameter("pageIndex");
        if (pageIndexstr != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            if (pageIndex > ((rowCount + pageSize - 1) / pageSize)) {
                pageIndex = ((rowCount + pageSize - 1) / pageSize);
            }
        }
        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);

        List<AdminInfo> adminList = adminDao.getAllAdmin(page);
        request.setAttribute("adminList", adminList);
        request.setAttribute("page", page);

        request.getRequestDispatcher("/admin/admin_manage.jsp").forward(request, response);
    }

    //查询用户名是否重复
    private void checkAdminName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String adminName = request.getParameter("adminName");

        AdminInfo admin = adminDao.checkName(adminName);

        if (admin == null) {
            response.getWriter().print("1");
        } else {
            response.getWriter().print("0");
        }
    }

    //添加用户
    private void addAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String adminName = request.getParameter("adminName");
        String password = request.getParameter("password");
        String note = request.getParameter("note");

        String state = "1";

        int roleId = 0;
        AdminInfo admin = new AdminInfo();
        admin.setAdminName(adminName);
        admin.setPassword(Des.encStr(password));
        admin.setNote(note);
        admin.setState(state);
        admin.setRoleId(roleId);

        int result = adminDao.addAdmin(admin);
        if (result == 1) {
            request.setAttribute("msg", "用户添加成功");
            request.getRequestDispatcher("/admin/admin_add.jsp").forward(request, response);
        }
    }
}
